﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Content;


namespace DotNetNuke.Modules.CashBook.Components
{
    public class CashLine:ContentItem 
    {
        ///<summary>
        /// LineId identifies an individual CashBook entry
        ///</summary>
        public int LineId { get; set; }

        ///<summary>
        /// The transaction date defaulted to today's date on page load
        /// in the entry form - shown in short date form
        ///</summary>
        public DateTime TransDate { get; set; }

        ///<summary>
        /// A string with the name of the person paying us
        ///</summary>
        public string WhoFrom { get; set; }

        ///<summary>
        /// A string with the name of the person we are paying
        ///</summary>
        public string WhoTo { get; set; }

        ///<summary>
        /// A string with the reason for payment
        ///</summary>
        public string Reason { get; set; }

        ///<summary>
        /// A string with the department who benefit
        /// from the payment
        ///</summary>
        public string WhoFor { get; set; }

        ///<summary>
        /// An single for the amount paid in
        ///</summary>
        public Single AmountIn { get; set; }

        ///<summary>
        /// An single for the amount paid out
        ///</summary>
        public Single AmountOut { get; set; }

        ///<summary>
        /// A boolean which indicates whether the accounts 
        /// department have cleared this transaction
        /// </summary>
        public bool Posted { get; set; }

        ///<summary>
        /// The ModuleId of where the task was created and gets displayed
        ///</summary>
        public int ModuleId { get; set; }

        ///<summary>
        /// An integer for the user id of the user who created the entry
        ///</summary>
        public int CreatedByUserId { get; set; }

        ///<summary>
        /// An integer for the user id of the user who last updated the entry
        ///</summary>
        public int LastModifiedByUserId { get; set; }

        ///<summary>
        /// The date the entry was created
        /// can be different to the date of
        /// the transaction
        ///</summary>
        public DateTime CreatedOnDate { get; set; }

        ///<summary>
        /// The date the entry was updated
        ///</summary>
        public DateTime LastModifiedOnDate { get; set; }

        ///<summary>
        /// The portal where the CashBook resides
        ///</summary>
        public int PortalId { get; set; }

        //Read Only Props
        ///<summary>
        /// The username of the user who created the task
        ///</summary>
        public string CreatedByUser
        {
            get
            {
                return CreatedByUserId != 0 ? Entities.Users.UserController.GetUserById(PortalId, CreatedByUserId).Username : Null.NullString;
            }
        }

        ///<summary>
        /// The username of the user who last updated the task
        ///</summary>
        public string LastUpdatedByUser
        {
            get
            {
                return LastModifiedByUserId != 0 ? Entities.Users.UserController.GetUserById(PortalId, LastModifiedByUserId).Username : Null.NullString;
            }
        }

        #region IHydratable Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        public override void Fill(System.Data.IDataReader dr)
        {
            //Call the base classes fill method to populate base class properties
            base.FillInternal(dr);

            LineId = Null.SetNullInteger(dr["LineId"]);
            TransDate = Null.SetNullDateTime(dr["TransDate"]);
            WhoFrom = Null.SetNullString(dr["WhoFrom"]);
            WhoTo = Null.SetNullString(dr["WhoTo"]);
            Reason = Null.SetNullString(dr["Reason"]);
            WhoFor = Null.SetNullString(dr["WhoFor"]);
            AmountOut = Null.SetNullSingle(dr["AmountOut"]);
            AmountIn = Null.SetNullSingle(dr["AmountIn"]);
            Posted = Null.SetNullBoolean(dr["Posted"]);
            ContentItemId = Null.SetNullInteger(dr["ContentItemId"]);
            ModuleId = Null.SetNullInteger(dr["ModuleId"]);
            CreatedByUserId = Null.SetNullInteger(dr["CreatedByUserId"]);
            CreatedOnDate = Null.SetNullDateTime(dr["CreatedOnDate"]);
            LastModifiedByUserId = Null.SetNullInteger(dr["LastModifiedByUserId"]);
            LastModifiedOnDate = Null.SetNullDateTime(dr["LastModifiedOnDate"]);
        }

        /// <summary>
        /// Gets and sets the Key ID
        /// </summary>
        /// <returns>An Integer</returns>
        public override int KeyID
        {
            get { return LineId; }
            set { LineId = value; }
        }

        #endregion
    }

    public class Total : ContentItem
    {
        ///<summary>
        /// TotalIn contains the aggregate sum of 
        /// CashBook AmountIn entries
        ///</summary>
        public Single TotalIn { get; set; }

        ///<summary>
        /// TotalOut contains the aggregate sum of
        /// CashBook AmountOut entries
        ///</summary>
        public Single TotalOut { get; set; }

        ///<summary>
        /// Balance contains the result of subtracting the 
        /// aggregate sum of CashBook AmountIn total
        /// from the CashBook AmountOut total
        ///</summary>
        public Single Balance { get; set; }

        #region IHydratable Implementation

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dr"></param>
        public override void Fill(System.Data.IDataReader dr)
        {
            //Call the base classes fill method to populate base class properties
            //base.FillInternal(dr);

            TotalIn = Null.SetNullSingle(dr["TotalIn"]);
            TotalOut = Null.SetNullSingle(dr["TotalOut"]);
            Balance = Null.SetNullSingle(dr["Balance"]);
        }

        #endregion

    }
}